﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeTier.DAL;

namespace CodeTier.BUL
{
    public class DanhMucBUS
    {
        public int MaDM { get; set; }
        public string TenDM { get; set; }
        public string MoTa { get; set; }
        //public int SoSPDangBan { get; set; }
        public DateTime NgayTao { get; set; }
        public string TinhTrang { get; set; }

        public DanhMucBUS() { }

        public DanhMucBUS(int madm) 
        {
            getByIdConvert(madm);
        }

        public bool insert()
        {
            DanhMuc dm = new DanhMuc
            {
                TenDM = this.TenDM,
                MoTa = this.MoTa,
                NgayTao = this.NgayTao,
                TinhTrang = this.TinhTrang,
            };
            CommonDAO.XDMH.DanhMucs.InsertOnSubmit(dm);
            try
            {
                CommonDAO.XDMH.SubmitChanges();
                CommonDAO.XDMH.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, CommonDAO.XDMH.DanhMucs);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool delete()
        {            
            CommonDAO.XDMH.DanhMucs.DeleteOnSubmit(getByIdNonConvert(this.MaDM));
            try
            {
                CommonDAO.XDMH.SubmitChanges();
                CommonDAO.XDMH.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, CommonDAO.XDMH.DanhMucs);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool update()
        {
            var result = getByIdNonConvert(this.MaDM);

            result.MoTa = this.MoTa;
            result.TenDM = this.TenDM;
            try
            {
                CommonDAO.XDMH.SubmitChanges();
                CommonDAO.XDMH.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, CommonDAO.XDMH.DanhMucs);
                return true;
            }
            catch (Exception)
            {
                return false;
            }        
        }

        public static DanhMuc getByIdNonConvert(int madm)
        {
            var result = (from dm in CommonDAO.XDMH.DanhMucs
                          where dm.MaDM == madm
                          select dm).SingleOrDefault();            
            return result;
        }

        public static DanhMucBUS getByIdConvert(int madm)
        {
            var result = getByIdNonConvert(madm);
            return convertUp(result);
        }

        public static DanhMucBUS convertUp(DanhMuc dm)
        {
            DanhMucBUS dm1 = new DanhMucBUS();
            dm1.MaDM = dm.MaDM;
            dm1.TenDM = dm.TenDM;
            dm1.MoTa = dm.MoTa;
            dm1.NgayTao = dm.NgayTao;
            return dm1;
        }        

        public static List<DanhMucBUS> getAll()
        {
            var results = (from dm in CommonDAO.XDMH.DanhMucs
                          select dm);
            List<DanhMucBUS> lstDM = new List<DanhMucBUS>();
            foreach (var result in results)
            {
                DanhMucBUS dmBus = new DanhMucBUS();
                dmBus.MaDM = result.MaDM;
                dmBus.TenDM = result.TenDM;
                dmBus.MoTa = result.MoTa;
                dmBus.NgayTao = result.NgayTao;
                lstDM.Add(dmBus);
            }
            return lstDM;
        }
    }
}
